package com.cyj.dream.admin.controller.admin.system;

import com.alibaba.fastjson.JSONObject;
import com.cyj.dream.admin.model.request.SysUserRoleRequest;
import com.cyj.dream.admin.model.system.SysUser;
import com.cyj.dream.admin.model.system.SysUserRole;
import com.cyj.dream.admin.service.system.SysUserRoleService;
import com.cyj.dream.admin.service.system.SysUserService;
import com.cyj.dream.core.aspect.annotation.ResponseResult;
import com.cyj.dream.core.aspect.annotation.log.LogRecord;
import com.cyj.dream.core.aspect.enums.OperatorType;
import com.cyj.dream.core.constant.Constant;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.exception.BizException;
import com.cyj.dream.core.util.IsEmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 用户角色关联关系控制器
 * @BelongsProject: fckj-restaurant
 * @BelongsPackage: com.fckj.fckjrestaurant.controller.admin.systems
 * @Author: ChenYongJia
 * @CreateTime: 2021-05-17 09:50
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@RestController
@ResponseResult
@Api(value = "userRole", tags = "用户角色关联模块")
@RequestMapping(value = "/pc/userRole", name = "用户角色关联模块")
public class SysUserRoleController {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 为用户设置角色不带分页
     *
     * @param userId 用户id
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/6/9 21:05
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "为用户设置角色不带分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Long", required = true)
    })
    @LogRecord(title = "为用户设置角色不带分页", operatetype = OperatorType.SELECT)
    @RequestMapping(value = "/getRoleList", name = "为用户设置角色不带分页", method = RequestMethod.GET)
    public Map<String, Object> getRoleList(@RequestParam(value = "userId", required = false) Long userId) {
        log.info("进入不分页查询角色控制器");
        if (IsEmptyUtils.isEmpty(userId)) {
            log.error("出现错误：参数缺失为用户设置角色失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setSysUserId(userId);

        // 根据用户详情id查询用户信息
        SysUser user = sysUserService.findBySysUserId(userId);
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        map.put("right", sysUserRoleService.selectSysUserRoleList(sysUserRole));
        map.put("left", sysUserRoleService.getRoleLists());
        return map;
    }

    /**
     * 为用户设置角色
     *
     * @param sysUserRoleRequest 用户角色关联模块设置角色使用
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/4/21 10:44
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "为用户设置角色", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUserRoleRequest", value = "用户角色关联模块设置角色使用", dataType = "SysUserRoleRequest", required = true)
    })
    @LogRecord(title = "为用户设置角色", operatetype = OperatorType.INSERT)
    @RequestMapping(value = "/settingSysUserRole", name = "为用户设置角色", method = RequestMethod.POST)
    public Map<String, Object> settingSysUserRole(@RequestBody SysUserRoleRequest sysUserRoleRequest) {
        log.info("进入为用户设置角色方法控制器，参数：{}", JSONObject.toJSONString(sysUserRoleRequest));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysUserRoleRequest.getSysUserId()) || IsEmptyUtils.isEmpty(sysUserRoleRequest.getSysRoleId())) {
            log.error("出现错误：参数缺失为用户设置角色失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        Set<Long> sysRoleId = new HashSet<>();
        String[] array = sysUserRoleRequest.getSysRoleId().split(",");
        for (String sysRoleIds : array) {
            sysRoleId.add(Long.parseLong(sysRoleIds));
        }

        map.put("info", sysUserRoleService.settingSysUserRole(sysUserRoleRequest.getSysUserId(), sysRoleId));
        return map;
    }

    /**
     * 为用户移除角色
     *
     * @param sysUserRoleRequest 用户角色关联模块设置角色使用
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/4/21 10:44
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "为用户移除角色", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUserRoleRequest", value = "用户角色关联模块设置角色使用", dataType = "SysUserRoleRequest", required = true)
    })
    @LogRecord(title = "为用户移除角色", operatetype = OperatorType.DELETE)
    @RequestMapping(value = "/deleteSysUserRole", name = "为用户移除角色", method = RequestMethod.DELETE)
    public Map<String, Object> deleteSysUserRole(@RequestBody SysUserRoleRequest sysUserRoleRequest) {
        log.info("进入为用户移除角色方法控制器，参数：{}", JSONObject.toJSONString(sysUserRoleRequest));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysUserRoleRequest.getSysUserId()) || IsEmptyUtils.isEmpty(sysUserRoleRequest.getSysRoleId())) {
            log.error("出现错误：参数缺失为用户移除角色失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        Set<Long> sysRoleId = new HashSet<>();
        String[] array = sysUserRoleRequest.getSysRoleId().split(",");
        for (String sysRoleIds : array) {
            sysRoleId.add(Long.parseLong(sysRoleIds));
        }

        map.put("info", sysUserRoleService.deleteSysUserRole(sysUserRoleRequest.getSysUserId(), sysRoleId));
        return map;
    }

}
